package org.linlinjava.litemall.admin.service;

import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

//@Service
public class MailService{
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
//    private UserMapper userMapper;

    public String sendMail(User user) {
        User user2 = mailPwd(user);
        StringBuilder stringBuilder=new StringBuilder();
        stringBuilder.append("<html><head><title></title></head><body>");
//        stringBuilder.append("亲爱的用户"+user2.getAccount()+",您的当前密码是："+user2.getPassword());
        stringBuilder.append("</br><a href='https://www.baidu.com/'>访问百度</a></body><html>");
        MimeMessage mimeMessage=javaMailSender.createMimeMessage();
        //multipart模式
        try {
            MimeMessageHelper mimeMessageHelper=new MimeMessageHelper(mimeMessage, true);
//            mimeMessageHelper.setTo(user.getMail());//收件人邮箱user.getMail()
            mimeMessageHelper.setFrom("sple_vip@163.com");//发件人邮箱
            mimeMessage.setSubject("找回密码");
            //启用html
            mimeMessageHelper.setText(stringBuilder.toString(),true);
            javaMailSender.send(mimeMessage);
            return null;
        } catch (MessagingException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 通过邮箱到数据库查询用户的信息
     */

    public User mailPwd(User user) {
        return null;//userMapper.mailPwdilPwd(user);
    }

}